package lab;

import java.util.Date;

import lab.dto.UserDetails;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateTest {
    public static void main(String[] args) {
        String identifier = "fdb";

        UserDetails user = new UserDetails();
        user.setUserName("First User");
        user.setDateOfBirth("20131018");
        user.setJoinedDate(new Date());

        UserDetails user2 = new UserDetails();
        user2.setUserName("Second User");
        user2.setJoinedDate(new Date());
        byte[] b = "123456789".getBytes();
        user2.setMyFile(b);

        Configuration cfg = new Configuration();
        cfg.setProperty("hibernate.multiTenancy", "DATABASE");

        SessionFactory sessionFactory = cfg.configure().buildSessionFactory();
        Session session = sessionFactory.withOptions().tenantIdentifier(
                identifier).openSession();
        session.beginTransaction();

        session.save(user);
        session.save(user2);

        session.getTransaction().commit();
        session.close();

        /*
         * Retrieve
         */
        user = null;
        session = sessionFactory.withOptions().tenantIdentifier(identifier)
                .openSession();
        session.beginTransaction();
        user = (UserDetails) session.get(UserDetails.class, 14);

        System.out.println(user.getUserName());
        System.out.println("file:"+new String(user.getMyFile()));
        System.out.println("Done");
    }
}
